// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.network.GameClient;
import zombie.util.StringUtils;

public class AttackNetworkState extends State {
    private static final AttackNetworkState s_instance = new AttackNetworkState();
    private String attackOutcome;

    public static AttackNetworkState instance() {
        return s_instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        hashMap.clear();
        hashMap.put(0, Boolean.FALSE);
        this.attackOutcome = owner.getVariableString("AttackOutcome");
        owner.setVariable("AttackOutcome", "start");
        owner.clearVariable("AttackDidDamage");
        owner.clearVariable("ZombieBiteDone");
        isoZombie.setTargetSeenTime(1.0F);
        if (!isoZombie.bCrawling) {
            isoZombie.setVariable("AttackType", "bite");
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        IsoGameCharacter isoGameCharacter = (IsoGameCharacter)isoZombie.target;
        if (isoGameCharacter == null || !"Chainsaw".equals(isoGameCharacter.getVariableString("ZombieHitReaction"))) {
            String string = owner.getVariableString("AttackOutcome");
            if ("success".equals(string)
                && !owner.getVariableBoolean("bAttack")
                && (isoGameCharacter == null || !isoGameCharacter.isGodMod())
                && !owner.getVariableBoolean("AttackDidDamage")
                && owner.getVariableString("ZombieBiteDone") != "true") {
                owner.setVariable("AttackOutcome", "interrupted");
            }

            if (isoGameCharacter == null || isoGameCharacter.isDead()) {
                isoZombie.setTargetSeenTime(10.0F);
            }

            if (isoGameCharacter != null
                && hashMap.get(0) == Boolean.FALSE
                && !"started".equals(string)
                && !StringUtils.isNullOrEmpty(owner.getVariableString("PlayerHitReaction"))) {
                hashMap.put(0, Boolean.TRUE);
            }

            isoZombie.setShootable(true);
            if (isoZombie.target != null && !isoZombie.bCrawling) {
                if (!"fail".equals(string) && !"interrupted".equals(string)) {
                    isoZombie.faceThisObject(isoZombie.target);
                }

                isoZombie.setOnFloor(false);
            }

            if (isoZombie.target != null) {
                isoZombie.target.setTimeSinceZombieAttack(0);
                isoZombie.target.setLastTargettedBy(isoZombie);
            }

            if (!isoZombie.bCrawling) {
                isoZombie.setVariable("AttackType", "bite");
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        owner.clearVariable("AttackOutcome");
        owner.clearVariable("AttackType");
        owner.clearVariable("PlayerHitReaction");
        owner.setStateMachineLocked(false);
        if (isoZombie.target != null && isoZombie.target.isOnFloor()) {
            isoZombie.setEatBodyTarget(isoZombie.target, true);
            isoZombie.setTarget(null);
        }

        isoZombie.AllowRepathDelay = 0.0F;
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        IsoZombie isoZombie = (IsoZombie)owner;
        if (GameClient.bClient && isoZombie.isRemoteZombie()) {
            if (event.m_EventName.equalsIgnoreCase("SetAttackOutcome")) {
                isoZombie.setVariable("AttackOutcome", "fail".equals(this.attackOutcome) ? "fail" : "success");
            }

            if (event.m_EventName.equalsIgnoreCase("AttackCollisionCheck") && isoZombie.target instanceof IsoPlayer isoPlayer) {
                if (isoZombie.scratch) {
                    isoZombie.getEmitter().playSoundImpl("ZombieScratch", isoZombie);
                } else if (isoZombie.laceration) {
                    isoZombie.getEmitter().playSoundImpl("ZombieScratch", isoZombie);
                } else {
                    isoZombie.getEmitter().playSoundImpl("ZombieBite", isoZombie);
                    isoPlayer.splatBloodFloorBig();
                    isoPlayer.splatBloodFloorBig();
                    isoPlayer.splatBloodFloorBig();
                }
            }

            if (event.m_EventName.equalsIgnoreCase("EatBody")) {
                owner.setVariable("EatingStarted", true);
                ((IsoZombie)owner).setEatBodyTarget(((IsoZombie)owner).target, true);
                ((IsoZombie)owner).setTarget(null);
            }
        }

        if (event.m_EventName.equalsIgnoreCase("SetState")) {
            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Attack);
        }
    }

    @Override
    public boolean isAttacking(IsoGameCharacter owner) {
        return true;
    }
}
